import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler

# load data
train = np.loadtxt(r'./x_oil_data/train.txt', delimiter=',')
test = np.loadtxt(r'./x_oil_data/test.txt', delimiter=',')

# scale
scaler = StandardScaler()
scaler.fit(train)
train = scaler.transform(train)
scaler = StandardScaler()
scaler.fit(test)
test = scaler.transform(test)

# prepare data
x = train[:, :-1]
y = train[:, -1]
x_test = test[:, :-1]
y_test = test[:, -1]

# fit data
model = LinearRegression()
model.fit(x, y)
theta0 = model.intercept_
theta1_n = model.coef_
print(theta0.shape, theta1_n.shape)
theta = np.r_[theta0, theta1_n]
print(f'theta = {theta}')
xscore = model.score(x, y)
print(f'score = {xscore}')

# test
h_test = model.predict(x_test)
xscore = model.score(x_test, y_test)
print(f'score = {xscore}')
plt.scatter(y_test, y_test, s=1, label='target values')
plt.scatter(y_test, h_test, s=1, label='hypothesis values')
plt.grid()
plt.legend()
plt.show()
